﻿using System.Collections.Generic;
using System.Linq;
using XSerialization.Generator;

namespace XSerialization.Templates
{
    public partial class ClassFileTemplate
    {
        /// <summary>
        /// List of interface declarations
        /// </summary>
        public IList<XsdComplexType> Classes { get; set; }

        /// <summary>
        /// Meta Informations
        /// </summary>
        public IFileMetaInfo MetaInfo { get; set; }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="classes">Class declarations</param>
        /// <param name="metaInfo">Meta Informations</param>
        /// <param name="interfaceMetaInfo">Interface Meta Informations</param>
        public ClassFileTemplate(IList<XsdComplexType> classes, IFileMetaInfo metaInfo, IFileMetaInfo interfaceMetaInfo)
        {
            MetaInfo = metaInfo;
            Classes = classes;

            bool contains = MetaInfo.Usings.Any(item => item == "System.Collections.Generic");
            if (!contains) MetaInfo.Usings.Add("System.Collections.Generic");
            contains = MetaInfo.Usings.Any(item => item == "System.Collections.ObjectModel");
            if (!contains) MetaInfo.Usings.Add("System.Collections.ObjectModel");
            contains = MetaInfo.Usings.Any(item => item == "XSerialization");
            if (!contains) MetaInfo.Usings.Add("XSerialization");
            if (interfaceMetaInfo.NameSpace != MetaInfo.NameSpace)
            {
                contains = MetaInfo.Usings.Any(item => item == interfaceMetaInfo.NameSpace);
                if (!contains) MetaInfo.Usings.Add(interfaceMetaInfo.NameSpace);
            }
        }

        /// <summary>
        /// Returns Class Template
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public string GetClassTemplate(XsdComplexType item)
        {
            ClassTemplate classTemplate = new ClassTemplate(item, MetaInfo);
            return classTemplate.TransformText();
        }
    }
}
